﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Winista.Text.HtmlParser;
using Winista.Text.HtmlParser.Util;
using Winista.Text.HtmlParser.Filters;
using Winista.Text.HtmlParser.Tags;

namespace seo
{
    class Collector
    {
        public virtual void Collect() { }

        public Hashtable GetUrlsFromUrl(string url)
        {
            HttpHelper hh = new HttpHelper();
            string html = hh.sendRequest(url);
            return GetUrlsFromHtml(html);
        }

        public virtual Hashtable GetUrlsFromHtml(string html)
        {
            Hashtable ht = new Hashtable();
            Parser parser = Parser.CreateParser(html, "utf-8");
            NodeFilter filter = new NodeClassFilter(typeof(ATag));
            NodeList list = parser.ExtractAllNodesThatMatch(filter);

            ISimpleNodeIterator iterator = list.Elements();
            while (iterator.HasMoreNodes())
            {
                INode node = iterator.NextNode();
                ATag a = (ATag)node;
                if (a.Link.Trim().Length > 0)
                {
                    ht.Add(a.Link, a.ToPlainTextString());
                }
            }
            return ht;
        }
    }
}
